package com.hctforgreen.greenservice;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Config;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.model.BaseEntity;

/**
 *新闻详情
 * 
 * @author ld 20131206
 * 
 */
@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
@SuppressWarnings("unused")
public class NewsDetailActivity extends ParentActivity {
	private WebView webview;
	private Thread thread;
	private String url;
	private ProgressBar pro;

	private View fav_view, share_view, apply_view;
	private ImageView fav_logo, signup_logo;
	private String msg_id;
	private String msg_title;
	private String img_url;
	private String customer_id;
	public String captcha;
	// private TextView content_txt;
	// private InfoDetailEntity info;
	private View bottom_view;

	private TextView fav_txt, signup_txt;
	final Handler myHandler = new Handler();

	/**
	 * 自定义的Android代码和JavaScript代码之间的桥梁类
	 * 
	 * @author 1
	 * 
	 */
	public class WebAppInterface {
		Context mContext;

		/** Instantiate the interface and set the context */
		WebAppInterface(Context c) {
			mContext = c;
		}

		/** Show a toast from the web page */
		// 如果target 大于等于API 17，则需要加上如下注解
		@JavascriptInterface
		public void showToast(String toast) {
			// Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
			Toast.makeText(mContext, toast, Toast.LENGTH_LONG).show();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_detail);
		msg_id = getIntent().getStringExtra("msg_id");
		msg_title = getIntent().getStringExtra("msg_title");
		img_url = getIntent().getStringExtra("img_url");
		// initTitleView();
		initSkinLayout();

		webview = (WebView) findViewById(R.id.web);
		webview.setVisibility(View.GONE);
		webview.getSettings().setJavaScriptEnabled(true);
		webview.addJavascriptInterface(new JavascriptInterface2(this),
				"imagelistner");
		pro = (ProgressBar) findViewById(R.id.pro);
		getInformationDetail();
	}

	/**
	 * 获取新闻详情
	 */
	private void getInformationDetail() {

	}

	/*
	 * ImageGetter imgGetter = new Html.ImageGetter() { public Drawable
	 * getDrawable(String source) { Drawable drawable = null; URL url; try { url
	 * = new URL(source); drawable = Drawable.createFromStream(url.openStream(),
	 * ""); //获取网路图片 } catch (Exception e) { return null; }
	 * drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
	 * .getIntrinsicHeight()); return drawable; } };
	 */
	@SuppressLint("SetJavaScriptEnabled")
	private void loadUrl(final String url) {

		/*
		 * thread = new Thread(new Runnable() {
		 * 
		 * @SuppressWarnings("deprecation")
		 * 
		 * @Override public void run() { webview.loadUrl(url); MyWebViewClient
		 * myWebView = new MyWebViewClient();
		 * webview.setWebViewClient(myWebView); } }); thread.start();
		 */
		webview.loadUrl(url);
		MyWebViewClient myWebView = new MyWebViewClient();
		webview.setWebViewClient(myWebView);

	}

	// js通信接口
	public class JavascriptInterface2 {

		private final Context context;

		public JavascriptInterface2(Context context) {
			this.context = context;
		}

		@JavascriptInterface
		public void openImage(String img) {
			Intent intent = new Intent();
			intent.putExtra("image", img);
			intent.setClass(context, ShowMegaImageActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			context.startActivity(intent);
		}
	}

	// 注入js函数监听
	private void addImageClickListner() {
		// 这段js函数的功能就是，遍历所有的img几点，并添加onclick函数，在还是执行的时候调用本地接口传递url过去
		webview.loadUrl("javascript:(function(){"
				+ "var objs = document.getElementsByTagName(\"img\"); " +

				"for(var i=0;i<objs.length;i++)  " + "{"
				+ "    objs[i].onclick=function()  " + "    {  "
				+ "        window.imagelistner.openImage(this.src);  "
				+ "    }  " + "}" + "})()");
	}

	// 监听
	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {

			return super.shouldOverrideUrlLoading(view, url);
		}

		@Override
		public void onPageFinished(WebView view, String url) {

			view.getSettings().setJavaScriptEnabled(true);

			super.onPageFinished(view, url);
			// html加载完成之后，添加监听图片的点击js函数
			addImageClickListner();
			// 加载完毕后，关闭图片阻塞
			// webview.getSettings().setBlockNetworkImage(false);
			webview.setVisibility(View.VISIBLE);
			pro.setVisibility(View.GONE);

		}

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			view.getSettings().setJavaScriptEnabled(true);

			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onReceivedError(WebView view, int errorCode,
				String description, String failingUrl) {

			super.onReceivedError(view, errorCode, description, failingUrl);
		}
	}

	private String user_id;// 员工登录则填写用户ID,客户登录则填写登录手机号

	private String user_type;// 0-员工，1-客户
	private final String model = "0";// 0-资讯，1-二手车

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK) {
			return;
		}

	}

}
